﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Medical_Store_Billing.Business_Login_Layer;

namespace Medical_Store_Billing
{
    public partial class Formquanlynhacungcap : Form
    {
        NhacungcapBLL m_nhacungcapbll = new NhacungcapBLL();
        int flag;
        public Formquanlynhacungcap()
        {
            InitializeComponent();
        }

        private void Formquanlynhacungcap_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = m_nhacungcapbll.Laydsnhacungcap();
            Default();
            btnLuu.Enabled = false;
            btnThem.Enabled = true;
            btnSua.Enabled = true;
        }
        private void Default()
        {
            txtmancc.Enabled = false;
            txtdienthoai.Enabled = false;
            txtdiachi.Enabled = false;
            txttenncc.Enabled = false;
           
        }

        private void Add()
        {
            txtmancc.Enabled = true;
            txtdienthoai.Enabled = true;
            txtdiachi.Enabled = true;
            txttenncc.Enabled = true;
           
        }
        private void Edit()
        {
            txtmancc.Enabled = false;
            txtdienthoai.Enabled = true;
            txtdiachi.Enabled = true;
            txttenncc.Enabled = true;
          
        }

        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int dong = e.RowIndex;

            txtmancc.Text = dataGridView1.Rows[dong].Cells[0].Value.ToString();
            txttenncc.Text = dataGridView1.Rows[dong].Cells[1].Value.ToString();
            txtdienthoai.Text = dataGridView1.Rows[dong].Cells[2].Value.ToString();
            txtdiachi.Text = dataGridView1.Rows[dong].Cells[3].Value.ToString();
           
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            flag = 1;
            Add();
            btnThem.Enabled = false;
            btnSua.Enabled = false;
            btnLuu.Enabled = true;
            txtdiachi.Text = "";
            txtdienthoai.Text = "";
            txttenncc.Text = "";
            txtmancc.Text = "";
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            flag = 2;
            Edit();
            btnThem.Enabled = false;
            btnSua.Enabled = false;
            btnLuu.Enabled = true;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("bạn có muốn lưu?", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                if (CheckNull() == true)
                {
                    if (flag == 1)
                    {
                        if (m_nhacungcapbll.Themncc(txtmancc.Text, txttenncc.Text, txtdienthoai.Text, txtdiachi.Text))
                            Formquanlynhacungcap_Load(sender, e);
                    }
                    if (flag == 2)
                    {
                        if (m_nhacungcapbll.Suancc(txtmancc.Text, txttenncc.Text, txtdienthoai.Text, txtdiachi.Text))
                            Formquanlynhacungcap_Load(sender, e);
                    }
                }
                else
                {
                    Formquanlynhacungcap_Load(sender, e);
                }
            }
            else
                Formquanlynhacungcap_Load(sender, e);
        }
        private bool CheckNull()
        {
            if (txtmancc.Text == "")
            {
                MessageBox.Show("Mã NCC ko đc rỗng");
                return false;
            }
            else if (txttenncc.Text == "")
            {
                MessageBox.Show("Tên NCC ko đc rỗng");
                return false;
            }
            else if (txtdienthoai.Text == "")
            {
                MessageBox.Show("Số đt ko đc rỗng");
                return false;
            }
            else if (txtdiachi.Text == "")
            {
                MessageBox.Show("Địa chỉ ko đc rỗng");
                return false;
            }
            
            else return true;
        }

        private void btntimkiem_Click(object sender, EventArgs e)
        {
            if (txttimkiem.Text == "")
            {
                MessageBox.Show("Nhập Tên Nhà Cung Cấp!");
                return;
            }
            else
            {

                dataGridView1.DataSource = m_nhacungcapbll.Timncc(txttimkiem.Text);
            }
        }

    }
}
